/*
 * Copyright@www.it1997.com.
 * @Author: 陈晓晨
 * @Date: 2020/6/1
 * Description:版权所有 QQ:376231445
 */

package com.it1997.it1997admin.service.impl;

import com.it1997.it1997admin.mapper.UserMapper;
import com.it1997.it1997admin.pojo.User;
import com.it1997.it1997admin.service.UserService;
import com.it1997.it1997admin.vo.AdminVo;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
@Log4j2
@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private PasswordEncoder passwordEncoder;
    @Override
    public User getUserByUserName(String username) {
        return userMapper.getUserByUserName(username);
    }

    @Override
    public List<AdminVo> getAdminList(Integer page,Integer limit) {
        Integer index=(page-1)*limit;
        Integer last=page*limit;
        return userMapper.getAdminList(index,last);
    }

    @Override
    public Integer addAdmin(AdminVo adminVo) {
        String rowPassword=adminVo.getPassword();
        String encodedPassword = passwordEncoder.encode(rowPassword);
        adminVo.setPassword(encodedPassword);
        SimpleDateFormat dateFormat=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        adminVo.setCreateTime(dateFormat.format(new Date()));
        adminVo.setStatus(1);
        try {
           //添加用户
            userMapper.addAdmin(adminVo);
            User user=userMapper.getUserByUserName(adminVo.getUserName());
            log.info("user:"+user);
            log.info("adminVo:"+adminVo);
           //用户绑定角色
           userMapper.bindRole(user.getId(),adminVo.getRoleId());
           return 1;
        }catch (Exception e){
            e.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return 0;
        }

    }

    @Override
    public AdminVo getAdminById(Integer userId) {

        return userMapper.getAdminById(userId);
    }

    @Override
    public Integer updateAdminById(AdminVo adminVo) {
        return userMapper.updateAdminById(adminVo);
    }

    @Override
    public Integer getAdminCount() {
        return userMapper.getAdminCount();
    }

    @Override
    public Integer deleteAdminById(List<AdminVo> adminVoList) {
        return userMapper.deleteAdminById(adminVoList);
    }

    @Override
    public List<AdminVo> searchAdmin(AdminVo adminVo) {
        Integer page=adminVo.getPage();
        Integer limit= adminVo.getLimit();
        Integer index=(page-1)*limit;
        Integer last=page*limit;
        return userMapper.searchAdmin(adminVo,index,last);
    }

    @Override
    public Integer getSearchAdminCount(AdminVo adminVo) {
        return userMapper.getSearchAdminCount(adminVo);
    }
}
